<!DOCTYPE html>
<html xmlns:sa="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8">
    <title>流程定义列表</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" th:href="@{/pear/component/pear/css/pear.css}"/>


</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">流程名称</label>
                    <div class="layui-input-inline">
                        <input type="text" name="flowName" placeholder="请输入流程名称" class="layui-input"
                               id="flowName">
                    </div>
                </div>

                <div class="layui-form-item layui-inline">
                    <button class="layui-btn layui-btn-md" lay-submit sa:hasPermission="flowDefinition_select"
                            lay-filter="form-query">
                        <i class="layui-icon layui-icon-search"></i>
                        查询
                    </button>
                    <button type="reset" class="layui-btn layui-btn-md" id="resetBtn">
                        <i class="layui-icon layui-icon-refresh"></i>
                        重置
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>

<div class="layui-card">
    <div class="layui-card-body">
        <table id="processDefinitionTable" lay-filter="processDefinitionTable"></table>
    </div>
</div>

<script type="text/html" id="form-toolbar">
    <button class="layui-btn  layui-btn-md" sa:hasPermission="flowDefinition_add" lay-event="add">
        <i class="layui-icon layui-icon-add-1"></i>
        新增
    </button>
</script>
<script type="text/html" id="processDefinitionBar">
    <button class="layui-btn  layui-btn-sm" sa:hasPermission="flowDefinition_update" lay-event="edit"><i
            class="layui-icon layui-icon-edit">编辑</i>
    </button>
    <!--    <button class="layui-btn layui-btn-danger layui-btn-sm" lay-event="remove"><i-->
    <!--            class="layui-icon layui-icon-delete">删除</i>-->
    <!--    </button>-->
</script>


<script th:src="@{/pear/component/layui/layui.js}"></script>
<script th:src="@{/pear/component/pear/pear.js}"></script>

<script th:inline="javascript">
    let baseURL = /*[[@{/}]]*/ '';
    layui.use(['table', 'layer', 'jquery'], function () {
        let table = layui.table;
        let $ = layui.jquery;
        let layer = layui.layer;


        let cols = [
            [
                {type: 'checkbox'},
                {field: 'id', title: 'ID', sort: true},
                {field: 'flowKey', title: '流程Key', sort: true},
                {
                    field: 'name',
                    title: '流程名称',
                    templet: function (d) {
                        return '<a href="javascript:void(0)" class="process-name" data-id="' + d.bpmnXml + '" style="color: #00feff">' + d.name + '</a>';
                    }
                },
                {fixed: 'right', title: '操作', toolbar: '#processDefinitionBar'}
            ]
        ]
        // 渲染表格
        table.render({
            elem: '#processDefinitionTable',
            url: baseURL + 'workflow/flowDefinition/list',
            type: 'get',
            toolbar: '#form-toolbar',
            page: true,
            limit: 10,
            limits: [10, 20, 30],
            request: {
                pageName: 'pageNum',
                limitName: 'pageSize'
            },
            cols: cols,
            parseData: function (res) {
                return {
                    "code": res.code,
                    "msg": res.msg,
                    "count": res.data.total,
                    "data": res.data.list,
                };
            },
            response: {
                statusCode: 1000
            }
        });
        table.on('tool(processDefinitionTable)', function (obj) {
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'edit') {
                window.edit(obj);
            }
        });

        table.on('toolbar(processDefinitionTable)', function (obj) {
            if (obj.event === 'add') {
                window.add(obj);
            }
        });
        // 操作栏


        window.add = function () {
            layer.open({
                type: 2,
                title: '新增流程图',
                shade: 0.1,
                area: ['100%', '100%'],
                content: 'bpmn_add'
            });
        }

        window.edit = function (obj) {
            const decodedArrayBuffer = atob(obj.data.bpmnXml);
            const decodedUint8Array = new Uint8Array(decodedArrayBuffer.length);
            for (let i = 0; i < decodedArrayBuffer.length; i++) {
                decodedUint8Array[i] = decodedArrayBuffer.charCodeAt(i);
            }

            // 使用 TextDecoder 解码为指定编码的字符串
            const decoder = new TextDecoder('utf-8'); // 指定编码格式为 GBK
            window.xml = decoder.decode(decodedUint8Array)
            window.id = obj.data.id
            layer.open({
                type: 2,
                title: '编辑流程图',
                shade: 0.1,
                area: ['100%', '100%'],
                content: 'bpmn_edit'
            });
        }

        window.remove = function (obj) {
            let data = obj.data;
            // 删除流程
            layer.confirm('确定要删除该流程定义吗？', function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: baseURL + 'workflow/flowable/deleteProcessDefinition',
                    type: 'POST',
                    data: {
                        deploymentId: data.deploymentId,
                        cascade: true,
                    },
                    success: function (response) {
                        layer.close(loading);
                        if (response.code === 1000) {
                            layer.msg(response.msg, {
                                icon: 1,
                                time: 2000
                            }, function () {
                                table.reload('processDefinitionTable');
                            });
                        } else {
                            layer.msg(response.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }

                    },
                    error: function (xhr, status, error) {
                        layer.msg('删除失败：' + error);
                    }
                });
            });

        }


        //重置按钮
        document.getElementById('resetBtn').addEventListener('click', function () {
            table.reload('processDefinitionTable', {
                page: {curr: 1}
            });
        });


        // 监听流程名称的点击事件
        $(document).on('click', '.process-name', function () {
            var bpmnXml = $(this).data('id');
            showProcessDiagram(bpmnXml);
        });

        // 显示流程图的函数
        function showProcessDiagram(bpmnXml) {

            const decodedArrayBuffer = atob(bpmnXml);
            const decodedUint8Array = new Uint8Array(decodedArrayBuffer.length);
            for (let i = 0; i < decodedArrayBuffer.length; i++) {
                decodedUint8Array[i] = decodedArrayBuffer.charCodeAt(i);
            }

            // 使用 TextDecoder 解码为指定编码的字符串
            const decoder = new TextDecoder('utf-8'); // 指定编码格式为 GBK
            window.xml = decoder.decode(decodedUint8Array)
            // 显示模态框
            layer.open({
                type: 2,
                area: ['80%', '60%'],
                title: '查看流程图',
                content: 'bpmn_view',
            });
        }
    });
</script>


</body>
</html>